﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Net;

namespace MixSNS
{
    public partial class MixSNS : Form
    {

        public MixSNS()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LoginWebBrowser.Url = new Uri("https://graph.renren.com/oauth/authorize?client_id=" + MixSNSSupport.APP_KEY + "&redirect_uri=http://graph.renren.com/oauth/login_success.html&response_type=code&display=popup&"+MixSNSSupport.SCOPE_VAR);

        }

        public void findCode(string myUrl)
        {
            int codePosition = myUrl.IndexOf(MixSNSSupport.CODE);
            if (codePosition != -1)
            {
                MixSNSSupport.code = myUrl.Substring(codePosition + MixSNSSupport.CODE.Length);
                MessageBox.Show(MixSNSSupport.code);
                MixSNSSupport.getAccessToken();
            }
        }
        private void LoginWebBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            if (LoginWebBrowser.Url != null)
            {
                MessageBox.Show(LoginWebBrowser.Url.ToString());
                if (MixSNSSupport.code == null)
                    findCode(LoginWebBrowser.Url.ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //LoginWebBrowser.Url = new Uri("https://graph.renren.com/renren_api/session_key?oauth_token=access_token");
            List<APIParameter> paras = new List<APIParameter> ();
            paras.Add(new APIParameter ("api_key",MixSNSSupport.APP_KEY));
            paras.Add(new APIParameter ("method" , "users.getInfo"));
            paras.Add(new APIParameter ("v","1.0"));
            paras.Add(new APIParameter ("uids","225897202"));
            paras.Add(new APIParameter ("fields", "sex,star,zidou,vip,birthday"));
            paras.Add(new APIParameter ("session_key", MixSNSSupport.session_key));
            paras.Add(new APIParameter ("sig",MixSNSSupport.getSig(paras)));
            string responData  = MixSNSSupport.httpPost(MixSNSSupport.RENREN_SERVER,MixSNSSupport.getStringFromParas(paras));
            System.Xml.XmlDocument myResponData= new System.Xml.XmlDocument();
            myResponData.LoadXml(responData);
            
            MessageBox.Show(myResponData.ToString());
            //HttpContext.Current.Response.Redirect("https://graph.renren.com/oauth/authorize?client_id=" + APPID + "&redirect_uri=http://graph.renren.com/oauth/login_success.html&response_type=code&display=popup");
        }

        private void setStatusButton_Click(object sender, EventArgs e)
        {
            string status = statusTextBox.Text;
            string placeID = placeIDTextBox.Text;
            MixSNSSupport.updateStatue(status,placeID);
        }

        private void createPlaceButton_Click(object sender, EventArgs e)
        {
            MixSNSSupport.getPlaceID("mixSNS 0.1", "M78星云", "奥特行星", "0.0", "0.0");
        }
    }
}
